<?php
    $base_path = $_SERVER['DOCUMENT_ROOT'] . '/nettbutikk';     //  endre /dev/nettbutikk til din path
    include_once $base_path . "/DB/db.php";
    include_once $base_path . "/class/userClass.php";

    
class CategoryClass {

    // Checks if a category exists:
    function categoryExists($category){
        global $db;
        $category_query = $this->getCategoryInfo($category);
        $row = $category_query->fetch();
        if($row){
            return true;
        }
        return false;
    }

    // Get all data in DB about a Category with an unique name:
     function getCategoryInfo($categoryName){
        global $db;
        $category_query = $db->query("
            SELECT *
            FROM itemcategory
            WHERE name = '$categoryName'
        ");
        return $category_query;
    }

    // Get all data in DB about a Category with an unique Id:
    function getCategoryInfoId($categoryId){
        global $db;
        $category_query = $db->query("
            SELECT *
            FROM itemcategory
            WHERE kid = '$categoryId'
        ");
        return $category_query;
    }

    // Get all data about all categorys:
    function getAllCategorys(){
        global $db;
        $allCategorys = $db->query("SELECT * FROM itemcategory");
        return $allCategorys;
    }

    // Get all data about all categorys that are not deleted:
    function getAllLivingCategorys(){
        global $db;
        $allCategorys = $db->query("
            SELECT *
            FROM itemcategory
            WHERE deleted = 0");
        return $allCategorys;
    }

    // Get all the items connected to a category:
    function getCategoryItems($category){
        global $db;
        $item_query = $db->query("
            SELECT *
            FROM item i
            WHERE EXISTS (
             SELECT *
             FROM itemcategory c
             WHERE i.kid = c.kid
             AND c.name =  '$category'
            )
        ");
        return $item_query;
    }

    // Insert a new category to DB:
    function insertNewCategory($name, $description){
        global $db;
        $categoryQuery="INSERT INTO itemcategory (name, description) VALUES('$name','$description')";
        $categoryInsert = $db->prepare($categoryQuery);
        $result = $categoryInsert->execute();
        return $result;
    }

    // Delete a category:
    function deleteCategory($catId){
        global $db;
        $categoryQuery="
        UPDATE itemcategory
        SET deleted=1
        WHERE kid='$catId'";
        $categoryDelete = $db->prepare($categoryQuery);
        $result = $categoryDelete->execute();
        return $result;
    }

    // Get the name of a category:
    function getCatName($catId){
        global $db;
        $category_query = $db->query("
            SELECT *
            FROM itemcategory
            WHERE kid = '$catId'
        ");
        $row = $category_query->fetch();
        return $row['name'];
    }
    
}

$cat = new CategoryClass();

?>
